package com.enclose;


import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.*;

public class dataBasePool {
    static  Connection connection;
    //1.创建静态数据库连接池
    static {
        try {
            //一个链接执行一个sql？
            ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
            connection  = comboPooledDataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public Statement getStatement(){
        try {
            Statement statement1 = connection.createStatement();
            return statement1;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
    //直接获取PreparedStatement
    public PreparedStatement preparedStatement(String sql){

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            return preparedStatement;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
//        1）PreparedStatement:直接需要sql
    }


    //5.关闭空间
    public void close(Statement statement, ResultSet resultSet){
        try {
            if (connection != null) {
                connection.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (resultSet != null) {
                resultSet.close();
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
